/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-07-09
*/
package com.rzico.market.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Supplier;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.SupplierService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.market.entity.MarketProduct;
import com.rzico.market.service.MarketProductService;
import com.rzico.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 * 第三方产品档案控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-07-09
 */
@Api(description = "第三方产品档案接口")
@RestController
@RequestMapping("/admin/marketProduct")
public class MarketProductController extends BaseController {

    @Autowired
    private MarketProductService marketProductService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SysSequenceService sysSequenceService;

    @Autowired
    private SupplierService supplierService;

    /**
     * 分页查询第三方产品档案
     *
     * @return
    */
    @ApiOperation("分页查询第三方产品档案")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "supplierId", value = "供应商", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "brandName", value = "品牌", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "sn", value = "编码", dataType = "String", paramType = "query")
    })
    public CommResult<MarketProduct> list(Pageable pageable, String supplierId, String name, String brandName, String sn, String isShelf) {
        Map<String, Object> params = new HashMap<String, Object>();
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        params = buildSortField(params, pageable);

        if (StringUtils.isNotEmpty(supplierId)) {
            params.put("supplierId", supplierId);
        }
        params.put("isList", 1);
        params.put("isMarketable", 1);
        params.put("saleState", 1);
        if (StringUtils.isNotEmpty(name)) {
            params.put("name", name);
        }
        if (StringUtils.isNotEmpty(brandName)) {
            params.put("brandName", brandName);
        }
        if (StringUtils.isNotEmpty(sn)) {
            params.put("sn", sn);
        }
        if (StringUtils.isNotEmpty(isShelf)) {
            params.put("isShelf", isShelf);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<MarketProduct> list = marketProductService.selectList(params);
        PageResult<MarketProduct> pageResult = new PageResult<MarketProduct>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条第三方产品档案
     *
     * @return
    */
    @ApiOperation("查询单条第三方产品档案")
    @GetMapping("/find/{id}")
    public CommResult<MarketProduct> find(@PathVariable String id) {
        MarketProduct result = marketProductService.findById(id);
        Map<String,Object> data = new HashMap<>();
        Map<String,Object> params = new HashMap<>();
        if (StringUtils.isNotEmpty(result.getGoodId())) {
            params.put("goodId",result.getGoodId());
        } else {
            params.put("id", id);
        }
        List<MarketProduct> list = marketProductService.selectList(params);
        data.put("products",list);
        data.put("article", result.getMetaAndContent());
        data.put("brandName", result.getBrandName());

        if (result.getSupplierId()!=null) {
            Supplier supplier = supplierService.findById(result.getSupplierId());
            if (supplier!=null) {
                data.put("supplierName", supplier.getName());
            }
        }
        return CommResult.success(data);

    }

    /**
     * 保存第三方产品档案
     *
     * @param marketProduct
     * @return
     */
    @ApiOperation("保存第三方产品档案")
    @PostMapping("/save")
    public CommResult<MarketProduct> save(@RequestBody MarketProduct marketProduct) {
        int affectCount = marketProductService.insert(marketProduct);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        MarketProduct result = marketProductService.findById(marketProduct.getId());
        return CommResult.success(result);

    }

    /**
     * 批量删除第三方产品档案
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除第三方产品档案", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除第三方产品档案,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<MarketProduct> del(@PathVariable String ids) {

        marketProductService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新第三方产品档案
     *
     * @param marketProduct
     * @return
     */
    @Log(desc = "更新第三方产品档案", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新第三方产品档案")
    @PostMapping("/update")
    public CommResult<MarketProduct> update(@RequestBody MarketProduct marketProduct) {
        int affectCount = marketProductService.updateByPrimaryKeySelective(marketProduct);
        if (affectCount <= 0){
            return CommResult.error();
        }
        MarketProduct result = marketProductService.findById(marketProduct.getId());
        return CommResult.success(result);
    }

    /**
     * 京东商品sku组合成单个商品
     *
     * @return
     */
    @ApiOperation("京东商品sku组合成单个商品")
    @PostMapping("/jdProductCompose")
    public CommResult<MarketProduct> jdProductCompose(@RequestBody List<Map<String, Object>> skuInfoList, String supplierId){
        Map<String, Object> params = new HashMap<String, Object>();
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        String code = sysSequenceService.generate("jdGoods");
        List<MarketProduct> list = marketProductService.jdProductCompose(skuInfoList, supplierId, code);
        return CommResult.success(list);
    }

    /**
     * 京东商品添加至系统商品库
     *
     * @return
     */
    @ApiOperation("京东商品添加至系统商品库")
    @PostMapping("/jdProductShelf")
    public CommResult<MarketProduct> jdProductShelf(@RequestBody List<String> skuList, String supplierId){
        Map<String, Object> params = new HashMap<String, Object>();
        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        List<MarketProduct> list = marketProductService.jdProductShelf(skuList, enterprise, supplierId);
        return CommResult.success(list);
    }


}
